package luo.Controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import luo.Service.FactorService;
import luo.entity.Export;
import luo.entity.Factor;
import luo.utils.AjaxResult;
import org.apache.ibatis.annotations.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Description:
 * @Author:let_free
 * @Date:2024/10/2/21:58
 **/
@RestController
@Slf4j
@RequestMapping("/factor")
@Api(tags = "站点气象要素管理")
public class FactorController {
    @Autowired
    private FactorService factorService;

    @Autowired
    private Export export;

    @GetMapping("/latestStationFactors")
    @ApiOperation(value = "获取最近站点气象要素")
    public AjaxResult latestStationFactors() {
        return AjaxResult.success(factorService.latestStationFactors());
    }

    @GetMapping("/getList")
    @ApiOperation(value = "获取站点气象要素列表")
    public AjaxResult getList(@RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime, @RequestParam Integer pageSize, @RequestParam Integer pageNum) {
        return AjaxResult.success(factorService.getList(pageNum, pageSize, startTime, endTime));
    }

    @GetMapping("/export")
    public AjaxResult export(@RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime, @RequestParam Integer pageSize, @RequestParam Integer pageNum) throws IOException {

        // 定义 Excel 文件的路径
        String fileName = export.getPath() + "/factor.xlsx";
        List<Factor> factors = factorService.getList(pageNum, pageSize, startTime, endTime).getRecords();

        // 使用 EasyExcel 写入数据
        EasyExcel.write(fileName, Factor.class).sheet("用户信息").doWrite(factors);
        return AjaxResult.success("导出成功", fileName);
    }

}
